package com.songshuai.webapi.baseapi.service.impl;

import com.songshuai.webapi.baseapi.common.settings.CacheKeys;
import com.songshuai.webapi.baseapi.dao.DictionaryDao;
import com.songshuai.webapi.baseapi.dto.feign.response.DictionaryDto;
import com.songshuai.webapi.baseapi.model.YixueBaseDictionary;
import com.songshuai.webapi.baseapi.service.BaseDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BaseDictionaryServiceImpl implements BaseDictionaryService {

    @Autowired
    private DictionaryDao dictionaryDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<DictionaryDto> getDictionaryByIds(List<Integer> ids) {
        List<DictionaryDto> retList = new ArrayList<>();
        if (ids == null || ids.size() == 0) {
            return retList;
        }
        ids=ids.stream().filter(f->f!=null&&f>0).distinct().collect(Collectors.toList());
        List<String> idsKeys=new ArrayList<>();
        for(Integer id:ids){
            String key= CacheKeys.DICTIONARY_CACHE_KEY+id;
            idsKeys.add(key);
        }
        ValueOperations<String,DictionaryDto> operations=redisTemplate.opsForValue();
        List<DictionaryDto> tempDictionarys =new ArrayList<>();
        try {
            tempDictionarys=operations.multiGet(idsKeys);
        }catch (Exception ex){
            log.error("get cache error",ex);
        }
        List<Integer>tempIds=new ArrayList<>();
        tempDictionarys.remove(null);
        if(tempDictionarys!=null&&tempDictionarys.size()>0){
            tempIds=tempDictionarys.stream().filter(c->c!=null&&c.getId()>0).map(DictionaryDto::getId).collect(Collectors.toList());
            retList=tempDictionarys;
            if(tempDictionarys.size()==ids.size()){
                return  retList;
            }
        }
        ids.removeAll(tempIds);
        List<YixueBaseDictionary> dics = dictionaryDao.getDictionarys(ids);
        HashMap<String,DictionaryDto>dicMap=new HashMap<>();
        for(YixueBaseDictionary dic:dics){
            DictionaryDto dicInfo=setDictionary(dic);
            retList.add(dicInfo);
            dicMap.put(CacheKeys.DICTIONARY_CACHE_KEY+dic.getId(),dicInfo);
        }
        try{
            operations.multiSet(dicMap);
        }catch (Exception ex){
            log.error("get cache error",ex);
        }
        return retList;
    }

    @Override
    public DictionaryDto getDictionaryById(Integer id) {
        if(id==null|| id<=0) {
            return null;
        }
        List<Integer>dicIds=new ArrayList<>();
        dicIds.add(id);
        List<DictionaryDto>dicList=getDictionaryByIds(dicIds);
        if(dicList!=null&&dicList.size()>0){
            return dicList.get(0);
        }
        return null;
    }

    @Override
    public String getDicNameById(Integer dicId) {
        String retData="";
        if(dicId<=0){
            return  retData;
        }
        DictionaryDto dicInfo=getDictionaryById(dicId);
        if(dicInfo!=null){
            retData=dicInfo.getName();
        }
        return retData;
    }

    @Cacheable(value = "dicByType")
    @Override
    public List<DictionaryDto> getDictionaryByType(Integer typeId) {
        List<DictionaryDto> retList=new ArrayList<>();
        if(typeId==null||typeId<=0){
            return retList;
        }
        List<YixueBaseDictionary>dictionaries=dictionaryDao.getDictionaryByType(typeId);
        if(dictionaries!=null&&dictionaries.size()>0){
            for (YixueBaseDictionary dic:dictionaries){
                retList.add(setDictionary(dic));
            }
        }
        return retList;
    }

    private DictionaryDto setDictionary(YixueBaseDictionary dic){
        DictionaryDto dictionaryDto=new DictionaryDto();
        dictionaryDto.setId(dic.getId());
        dictionaryDto.setName(dic.getName());
        dictionaryDto.setSort(dic.getSort());
        dictionaryDto.setType(dic.getType());
        return  dictionaryDto;
    }

    @Override
    public Map<Integer, DictionaryDto> setDictionary(List<DictionaryDto> dics) {
        Map<Integer, DictionaryDto> dicMap=new HashMap<>();
        if(dics==null||dics.size()==0){
            return dicMap;
        }
        for (DictionaryDto dic:dics){
            dicMap.put(dic.getId(),dic);
        }
        return dicMap;
    }

    @Override
    public List<DictionaryDto> getDictionaryByOldIds(List<String> oldIds) {
        List<DictionaryDto> retList=new ArrayList<>();
        if(oldIds==null||oldIds.size()==0){
            return  retList;
        }
        List<YixueBaseDictionary>dictionaries=dictionaryDao.getDictionByoldIds(oldIds);
        if(dictionaries!=null){
            for(YixueBaseDictionary dic:dictionaries){
                retList.add(setDictionary(dic));
            }
        }
        return retList;
    }

}
